<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of SettingsController
 *
 * @author Admin
 */
class Back_SettingsController extends Zend_Controller_Action
{
    public function init() {
        $this->_helper->layout->setLayout('admin');
    }
    
    public function systemAction() {
        $settings = new Back_Model_Settings();
        
        $systemSettings = $settings->getSystemSettings();
        $this->view->systemSettings = $systemSettings;
        
        $allAdvertisers = $settings->getAllAdvertisers();
        
        foreach ($allAdvertisers as $key => $adver) {
            $commission = $settings->getCommissionSelectAdver($adver['id']);
            $allAdvertisers[$key]['commission'] = $commission['commission'];
        }
        $this->view->allAdvertisers = $allAdvertisers;
        
        if ($this->getRequest()->isPost()) {
            $advertisers = $this->getRequest()->getPost('select');
            $rate = $this->getRequest()->getPost('rate');            
            foreach ($advertisers as $advertiser) {
                $data = array(
                    'commission'    => $rate,
                    'id_advertiser' => $advertiser
                );
                $count = $settings->getCountCommissionSelectAdver($advertiser);
                //если у выбранного юзера еще нету личной комиссии
                if ($count['count'] == 0) {
                    //делаем новую запись
                    $settings->addCommissionToSelectAdver($data);
                  //если уже была личная комиссия
                } else {
                    //редактируем старую
                    $settings->updateCommissionSelectAdver($advertiser, $data);
                }  
            }
            $this->_helper->redirector('system', 'settings');
        }
    }
    
    public function updatesystemAction() {
        $settings = new Back_Model_Settings();
        if ($this->getRequest()->isPost()) {
            $post = $this->getRequest();
            if ($post->getPost('submit_exchange_rate')) {
                $data = array(
                    'exchange_rate' => $post->getPost('exchange_rate')
                );                
            } elseif ($post->getPost('submit_commission_context')) {
                $data = array(
                    'commission_context' => $post->getPost('commission_context')
                );
            } elseif ($post->getPost('submit_coefficient_bid')) {
                $data = array(
                    'coefficient_bid' => $post->getPost('coefficient_bid')
                );
            } elseif ($post->getPost('submit_coefficient_prescription')) {
                $data = array(
                    'coefficient_prescription' => $post->getPost('coefficient_prescription')
                );
            } elseif ($post->getPost('submit_coefficient_geolocation')) {
                $data = array(
                    'coefficient_geolocation' => $post->getPost('coefficient_geolocation')
                );
            } elseif ($post->getPost('submit_coefficient_keywords')) {
                $data = array(
                    'coefficient_keywords' => $post->getPost('coefficient_keywords')
                );
            } elseif ($post->getPost('submit_coefficient_marketing_module')) {
                $data = array(
                    'coefficient_marketing_module' => $post->getPost('coefficient_marketing_module')
                );
            } elseif ($post->getPost('submit_safe_level')) {
                $data = array(
                    'safe_level' => $post->getPost('safe_level')
                );
            }
            $settings->updateSystemSettings($data);
            $this->_helper->redirector('system', 'settings');
        }               
    }
    
    public function updaterateadvAction() {
        $settings = new Back_Model_Settings();
        
    }
    
    public function generationAction() {
        $settings = new Back_Model_Settings();
        $allBanners = $settings->getAllSizeBanners();
        $this->view->allBanners = $allBanners;
        
        $contextSettings = $settings->getAllContextSettings();
        $this->view->contextSettings = $contextSettings;
    }
    
    public function savebannerAction() {
        $settings = new Back_Model_Settings();
        if ($this->getRequest()->isPost()) {            
            $checkbox = $this->getRequest()->getPost('check2');
            $checkbox == 'on'?$mobile = 'yes':$mobile = 'no';
            $data = array(
                'width'  => $this->getRequest()->getPost('width'),
                'height' => $this->getRequest()->getPost('height'),
                'max_memory_size' => $this->getRequest()->getPost('max_memory_size'),
                'mobile' => $mobile
            );                        
            $settings->saveBanner($data);
            $this->_helper->redirector('generation', 'settings');
        } 
    }
    
    public function deletebannerAction() {
        $idBanner = $this->_getParam('id');
        $settings = new Back_Model_Settings();
        $settings->deleteBanner($idBanner);
        $this->_helper->redirector('generation', 'settings');
    }
    
    public function updatebannerAction() {
        $idBanner = $this->_getParam('id');
        $settings = new Back_Model_Settings();
        $selectBanner = $settings->getSelectBanner($idBanner);
        $this->view->selectBanner = $selectBanner;
        if ($this->getRequest()->isPost()) {
            $checkbox = $this->getRequest()->getPost('check2');
            $checkbox == 'on'?$mobile = 'yes':$mobile = 'no';
            $data  = array(
                'height'          => $this->getRequest()->getPost('height'),
                'width'           => $this->getRequest()->getPost('width'),
                'max_memory_size' => $this->getRequest()->getPost('max_memory_size'),
                'mobile'          => $mobile
            );
            $settings->updateSelectBanner($idBanner, $data);
            $this->_helper->redirector('generation', 'settings');
        }
    }
    
    public function savecontextAction() {
        $settings = new Back_Model_Settings();
        if ($this->getRequest()->isPost()) {
            $data = array(
                'max_header_length'        => $this->getRequest()->getPost('max_header_length'),
                'min_adver_length'         => $this->getRequest()->getPost('min_adver_length'),
                'max_adver_length'         => $this->getRequest()->getPost('max_adver_length'),
                'max_font_size'            => $this->getRequest()->getPost('max_font_size'),
                'mobile_max_header_length' => $this->getRequest()->getPost('mobile_max_header_length'),
                'mobile_min_adver_length'  => $this->getRequest()->getPost('mobile_min_adver_length'),
                'mobile_max_adver_length'  => $this->getRequest()->getPost('mobile_max_adver_length'),
                'mobile_max_font_size'     => $this->getRequest()->getPost('mobile_max_font_size')
            );
            $settings->updateContext($data);
            $this->_helper->redirector('generation', 'settings');
        }
    }
}

?>
